package com.aabte.permissionframeworklearn.permission.controller;

import com.aabte.permissionframeworklearn.permission.common.ResponseBean;
import com.aabte.permissionframeworklearn.permission.exception.ParamException;
import com.aabte.permissionframeworklearn.permission.exception.PermissionException;
import com.aabte.permissionframeworklearn.permission.mapper.SysAclModuleMapper;
import com.aabte.permissionframeworklearn.permission.model.SysAclModule;
import com.aabte.permissionframeworklearn.permission.param.TestVo;
import com.aabte.permissionframeworklearn.permission.util.ApplicationContextHelper;
import com.aabte.permissionframeworklearn.permission.util.BeanValidator;
import com.aabte.permissionframeworklearn.permission.util.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Daniel
 */
@Slf4j
@Controller
@RequestMapping("/test")
public class TestController {

    @GetMapping("/hello.json")
    @ResponseBody
    public ResponseBean hello() {
        log.info("Start hello.");
        if (System.currentTimeMillis() % 3 == 0) {
            throw new PermissionException("PermissionException");
        }
        if (System.currentTimeMillis() % 3 == 1) {
            throw new RuntimeException("RuntimeException");
        }
        return ResponseBean.success("hello, permission");
    }

    @GetMapping("/validate.json")
    @ResponseBody
    public ResponseBean validate(
            TestVo vo
    ) throws ParamException {
        log.info("Start validate.");

        SysAclModuleMapper aclModuleMapper = ApplicationContextHelper.popBean(SysAclModuleMapper.class);
        SysAclModule sysAclModule = aclModuleMapper.selectByPrimaryKey(1L);
        log.info("sysAclModule: {}", JsonMapper.obj2String(sysAclModule));

        BeanValidator.check(vo);
        return ResponseBean.success("test BeanValidator");
    }

}
